package exc;

import java.util.Scanner;
/** binary search*/
public class BinarySearch{
	public static void main(String[] args){
		BinarySearch bs = new BinarySearch();
		int[] sour = bs.sortBub(new String[]{"11", "91", "18", "58", "25", "34", "54", "76", "48", "17"});
		
		for(int i=0; i<sour.length; i++){
			System.out.print(sour[i] + "  ");
		}
		System.out.println();
		System.out.println("Now, please enter the Number which you want search:");
		Scanner sc = new Scanner(System.in);
		int desc = sc.nextInt();
		
		System.out.println(bs.search(sour, desc));
	}

	public int[] sortBub(String[] args){
		if(args.length == 0){
			System.exit(0);
		}
		int[] sour = new int[args.length];
		for(int i=0; i<args.length; i++){
			sour[i] = Integer.parseInt(args[i]);
		}

		/* start sort*/
		for(int i=sour.length - 1; i>0; i--){
			for(int j=0; j<i; j++){
				if(sour[j] > sour[j+1]){
					sour[j+1] = sour[j] + sour[j+1];
					sour[j] = sour[j+1] - sour[j];
					sour[j+1] = sour[j+1] - sour[j];
				}
			}
		}
		return sour;
	}

	public String search(int[] sour, int desc){
		int low = 0;
		int high = sour.length - 1;
		while (low <= high) {
			if (sour[(high + low) / 2] == desc) {
				return "Finding, the index is:" + (high + low) / 2 + ", " + ((high + low) / 2 + 1) + "th number";
			} else {
				if (sour[(high + low) / 2] < desc) {
					low = (high + low) / 2 + 1;
				} else {
					high = (high + low) / 2 - 1;
				}
			}
		}
		return "Sorry, not found!";
	}
}